package com.lab.service.impl;

import com.lab.dao.StudentDao;
import com.lab.pojo.Student;
import com.lab.service.StudentService;
import com.lab.util.Md5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("studentService")
public class StudentServiceImp implements StudentService {
    @Resource
    StudentDao studentDao;
    //查询所有数据
    public List<Student> allStudents() {
        return studentDao.allStudents();
    }
    //添加和修改
    public void saveStu(Student stu) {
        try {
            Student s= studentDao.findStuById(stu.getSid());
            if (s!=null){
                //修改
                studentDao.updateStudent(stu);
            }else {
                //增加
                // Md5对密码加密
                stu.setPassword(Md5Util.encodeByMd5(stu.getPassword()));
                SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
                sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
                Date date = new Date();
                stu.setCreate_time(sdf.format(date));
                studentDao.addStudent(stu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除
    public void deleteStudent(Long[] sid) {
        studentDao.deleteStudent(sid);
    }

    public Student findStuById(Long sid) {
        return studentDao.findStuById(sid);
    }

    //登录
    public Student findStudent(Long sid) {
        return studentDao.findStudent(sid);
    }
}
